package com.uco.map;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<People> peopleList = new ArrayList<>();
        peopleList.add(new People("张三丰", 19));
        peopleList.add(new People("张无忌", 18));
        peopleList.add(new People("周芷若", 17));
        peopleList.add(new People("小昭", 16));

        // 生成 stream 流  --> stream 流的中间处理方法 --> stream 流的最终处理方法
        peopleList.stream().filter(p -> p.getName().startsWith("张")).forEach(System.out::println);

        // 最终方法收集 stream 成 list
        List<People> finalPeopleList = peopleList.stream().filter(p -> p.getName().startsWith("张")).collect(Collectors.toList());
        System.out.println(finalPeopleList);

        // 最终方法收集 stream 成 map
        Map<String, Integer> finalPeopleList2 = peopleList.stream().filter(p -> p.getName().startsWith("张")).collect(Collectors.toMap(People::getName, People::getAge));
        System.out.println(finalPeopleList2);

    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class People {
    public String name;
    public int age;

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
